package com.nuoniu.sibanyun.service.erp.in.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.bo.UsedGradeRuleBo;
import com.nuoniu.sibanyun.entity.erp.in.InInventoryInfo;
import com.nuoniu.sibanyun.entity.erp.in.InInventoryLocation;
import com.nuoniu.sibanyun.entity.erp.in.InWarehouse;
import com.nuoniu.sibanyun.mapper.erp.in.InInventoryLocationMapper;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryInfoService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryLocationService;
import com.nuoniu.sibanyun.service.erp.in.IInInventoryService;
import com.nuoniu.sibanyun.service.inOut.IVInOutRecordDetailService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 货位 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-22
 */
@Service
public class InInventoryLocationServiceImpl extends ServiceImpl<InInventoryLocationMapper, InInventoryLocation> implements IInInventoryLocationService {

    @Autowired
    private IInInventoryService inventoryService;
    @Autowired
    private IVInOutRecordDetailService inOutRecordDetailService;
    @Autowired
    private IInInventoryInfoService inInventoryInfoService;

    @Override
    public Page<InInventoryLocation> pageList(Page pages, InInventoryLocation location) {
        UserInfo userInfo = UserThreadLocal.get();
        String keyWord = location.getKeyWord();
        QueryWrapper<InInventoryLocation> queryWrapper = new QueryWrapper<>(location);
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        if (StringUtils.isNotBlank(keyWord)) {
            queryWrapper.and(w -> w.like("warehouse_name", keyWord).or().like("code", keyWord).or().like("name", keyWord));
        }
        queryWrapper.orderByDesc("create_date");

        Page page = page(pages, queryWrapper);
        return page;
    }

    @Override
    @Async
    public void updateWarehouse(InWarehouse inWarehouse) {
        baseMapper.updateWarehouse(inWarehouse);
        inventoryService.updateWarehouse(inWarehouse);
        inOutRecordDetailService.updateWarehouse(inWarehouse);
        inInventoryInfoService.updateFromInWarehouse(inWarehouse);
    }

    @Override
    public List<InInventoryLocation> getByWarehouse(String inWarehouseIds) {
        return baseMapper.getByWarehouse(inWarehouseIds);
    }

    @Override
    public List<Integer> getByWarehouseId(Integer warehouseId) {
        return baseMapper.getByWarehouseId(warehouseId);
    }

    @Override
    public InInventoryLocation getByCode(InInventoryLocation location) {
        QueryWrapper<InInventoryLocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", location.getCompanyId());
        queryWrapper.eq("code", location.getCode());
        if (null != location.getId()) {
            queryWrapper.ne("id", location.getId());
        }
        queryWrapper.last("LIMIT 1");
        return this.getOne(queryWrapper, false);
    }

    @Override
    public InInventoryLocation getByName(InInventoryLocation location) {
        QueryWrapper<InInventoryLocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", location.getCompanyId());
        queryWrapper.eq("name", location.getName());
        if (null != location.getId()) {
            queryWrapper.ne("id", location.getId());
        }
        queryWrapper.last("LIMIT 1");
        return this.getOne(queryWrapper, false);
    }

    @Override
    public UsedGradeRuleBo getMaxRuleLevel() {

        return null;
    }

    /**
     * 批量同步 货位名称 和 货位编码
     *
     * @param location
     */
    @Override
    public void batchUpdateFromInInventoryLocation(InInventoryLocation location) {
        baseMapper.batchUpdateFromInInventoryLocation(location);
    }

    @Override
    public void batchDelete(List<Integer> ids) {

    }
//    @Override 参考一下这个代码
//    public Page<InInventoryCheckSearchVo> pageInInventoryCheckSearchVo(Page page, InInventoryInfoSearchDto inInventoryInfoSearchDto) {
//        UserInfo userInfo = UserThreadLocal.get();
//        Page<InInventoryCheckSearchVo> inInventoryCheckSearchVoPage = baseMapper.pageInInventoryCheckSearchVo(page, userInfo.getCompanyId(), inInventoryInfoSearchDto);
//        return inInventoryCheckSearchVoPage;
//    }
    //todo zy编写可以删除
    @Override
    public Page<InInventoryLocation> pageList1(Page pages, InInventoryLocation location) {
        UserInfo userInfo = UserThreadLocal.get();
        String keyWord = location.getKeyWord();
        QueryWrapper<InInventoryLocation> queryWrapper = new QueryWrapper<>(location);
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        if (StringUtils.isNotBlank(keyWord)) {
            queryWrapper.and(w -> w.like("warehouse_name", keyWord).or().like("code", keyWord).or().like("name", keyWord));
        }
        queryWrapper.orderByDesc("create_date");
        Page page = page(pages, queryWrapper);
        return page;
    }
}
